package net.zoneland.knowledge.event;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * .
 *
 * @author: bcc
 * 2021/6/3
 */
@Configuration
public class EventBusConfig {

    @Autowired
    private QaRecordEventListener qaRecordEventListener;

    @Bean("qaRecordEventBus")
    public EventBus  qaRecordEventBus() {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors(), 0L, TimeUnit.SECONDS, new LinkedBlockingDeque(),
                new ThreadFactoryBuilder().setNameFormat("qaRecordEventBus-pool-%d").build()
        );
        EventBus eventBus = new AsyncEventBus(poolExecutor);
        eventBus.register(qaRecordEventListener);
        return eventBus;
    }
}
